<?php
namespace app\admin\controller\goods;

use app\common\model\Goods;
use app\common\model\Store;
use app\common\model\InStore;
use app\common\model\OutStore;

use app\admin\classes\BasicManager;
use core\Api;
use core\V;
use core\Res;

/**
 * 出入库
 */


 class StoreAct extends \C {


   public function index(){



     return view("goods/StoreAct",$this->VD());

   }
   // 获取所有快递公司列表
  public function getAllGoods(){
    $all=Goods::where("status",1)
              ->column('name','code');
    $result=[];
    foreach ($all as $key => $value) {
      $result[]=[
        "value"=>(string)$key,
        "label"=>$key."($value)",
      ];
    }

    Api::ok($result);
  }

   public function getAllStore(){
      $all=Store::where("status",1)
                ->column('name','id');
      $result=[];

      foreach ($all as $key => $value) {
        $result[]=[
          "value"=>(string)$key,
          "label"=>$value,
        ];
      }

      Api::ok($result);
   }

   public function save(){
     $data=input("data");


     $num=V::empty($data["num"],"0");
     $goods_code=V::empty($data["code"]);
     empty($goods_code) and Api::error("产品编码错误");
     $goods_id=Goods::where("code",$goods_code)->value("id");
     empty($goods_id) and Api::error("没有找到产品");

     $store_id=V::empty($data["store"]);
     empty($store_id) and Api::error("库位错误");



     $query=false;
     if ($data["type"]=="in"){
       $query=new InStore();
     }elseif ($data["type"]=="out") {
       $query=new OutStore();
     }else{
       Api::error("出入库类型错误");
     }

     ($query) or Api::error("遇到错误");
     $query->save([
       "goods_id"=>$goods_id,
       "store_id"=>$store_id,
       "num"=>$num,
       "user_id"=>$this->user->id(),
     ]) or Api::error("遇到错误");

     Api::ok("保存成功");
   }


 }
